package com.yjhb.android.sbz.mvp.recharge;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.yjhb.android.sbz.entity.AppPayParam;
import com.yjhb.android.sbz.entity.WxPayParam;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

import java.math.BigDecimal;

/**
 * 创建者 zx
 * 创建时间 2020/12/3 11:24
 */
public class RechargePresenter extends ConventionPresenter<RechargeView> {
    private final String TAG = "RechargePresenter";

    public RechargePresenter(RechargeView rechargeView) {
        super(rechargeView);
    }

    @Override
    protected void onDestroy() {

    }

    /**
     * 支付宝支付
     *
     * @param type   1、支付宝支付；2、微信支付
     * @param money  金额
     * @param target 目标
     */
    public void pay(int type, BigDecimal money, Integer target) {
        if (view != null) {
            view.onWait();
        }
        HttpTool.request(model.aliPay(type, money, target), new HttpObserver<AppPayParam>() {
            @Override
            public void onResult(AppPayParam data) {
                if (view != null) {
                    if (data.getPayType() == 1) {
                        view.onSuccessAli(data.getData().toString());
                    } else if (data.getPayType() == 2) {
                        view.onSuccessWx(new Gson().fromJson(data.getData().toString(), WxPayParam.class));
                    }
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, "充值失败："+msg);
                }
            }
        });
    }


    public void aliCancelOrder(String orderId) {
        if (model != null)
            HttpTool.request(model.cancelOrder(orderId), new HttpObserver<JsonObject>() {
                @Override
                public void onResult(JsonObject data) {
                    if (view != null) {
                        view.onAliCancel();
                    }
                }

                @Override
                public void onError(int code, String msg) {

                }
            });
    }


    public void specifyConfig(int type){
        if (model!=null){
            HttpTool.request(model.specifyConfig(type), new HttpObserver<JSONObject>() {
                @Override
                public void onResult(JSONObject data) {
                    if (view!=null){
                        view.onDisplay(data.getBoolean("alipay"),data.getBoolean("wechat"));
                    }
                }

                @Override
                public void onError(int code, String msg) {
                    if (view != null) {
                        view.onFailure(code, msg);
                    }
                }
            });
        }
    }

}
